package com.sh.studytest.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sh.studytest.R;
import com.sh.studytest.bean.SubscriptionOption;

import java.util.List;

public class SubscriptionAdapter extends RecyclerView.Adapter<SubscriptionAdapter.SubscriptionViewHolder> {
    private List<SubscriptionOption> subscriptionOptions;
    private Context context;

    public SubscriptionAdapter(Context context, List<SubscriptionOption> subscriptionOptions) {
        this.context = context;
        this.subscriptionOptions = subscriptionOptions;
    }

    @NonNull
    @Override
    public SubscriptionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_price, parent, false);
        return new SubscriptionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull SubscriptionViewHolder holder, int position) {
        SubscriptionOption option = subscriptionOptions.get(position);
        holder.textViewDuration.setText(option.getDuration());
        holder.textViewPrice.setText(String.format("¥%.2f", option.getPrice()));
        holder.textViewDailyPrice.setText(String.format("¥%.2f/日", option.getDailyPrice()));
    }

    @Override
    public int getItemCount() {
        return subscriptionOptions.size();
    }

    public static class SubscriptionViewHolder extends RecyclerView.ViewHolder {
        TextView textViewDuration;
        TextView textViewPrice;
        TextView textViewDailyPrice;

        public SubscriptionViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewDuration = itemView.findViewById(R.id.text_view_duration);
            textViewPrice = itemView.findViewById(R.id.text_view_price);
            textViewDailyPrice = itemView.findViewById(R.id.text_view_daily_price);
        }
    }
}